package com.zhihaoliang.animationtrain.main

import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import com.zhihaoliang.animationtrain.R
import com.zhihaoliang.animationtrain.annotation.Info


private const val TAG = "MainActivity"

class MainActivity : AppCompatActivity(), IInfoCallback {

    private val mInfoBeanList: MutableList<InfoBean> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        initActivityList()
        val recyclerView = findViewById<RecyclerView>(R.id.rcl_main)

        recyclerView.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(this)
                .color(Color.LTGRAY)
                .sizeResId(R.dimen.divider)
                .marginResId(R.dimen.divider_margin, R.dimen.divider_margin)
                .build()
        )

        recyclerView.adapter = MainAdapter(mInfoBeanList, this)
    }

    override fun onClick(infoBean: InfoBean) {
        startActivity(infoBean.intent)
    }

    private fun initActivityList() {
        val packageInfo = packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        for (info in packageInfo.activities) {
            val clazz = Class.forName(info.name)
            val hasAnnotation: Boolean =
                clazz.isAnnotationPresent(Info::class.java)
            Log.e(TAG, clazz.name)
            if (hasAnnotation) {
                val info = clazz.getAnnotation(Info::class.java)!!
                val infoBean = InfoBean(info.title, info.desc)
                infoBean.intent = Intent(this, clazz)
                mInfoBeanList.add(infoBean)
            }
        }
    }


}
